﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Travel.API.Models;

namespace Travel.API.DataBase
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {

        }
        public DbSet<TouristRoute> TouristRoute { get; set; }
        public DbSet<TouristRoutePhoto> TouristRoutePhoto { get; set; }




        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //种子数据
            //通过josn文件的方式 形成种子数据
            var touristRoutesJsonData = File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/DataBase/touristRoutesMockData.json");
            IList<TouristRoute> touristRoutes = JsonConvert.DeserializeObject<IList<TouristRoute>>(touristRoutesJsonData);
            modelBuilder.Entity<TouristRoute>().HasData(touristRoutes);

            // seed tourist toutes images
            var touristRoutePicturesJsonData = File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/DataBase/touristRoutePicturesMockData.json");
            IList<TouristRoutePhoto> touristRoutePictures = JsonConvert.DeserializeObject<IList<TouristRoutePhoto>>(touristRoutePicturesJsonData);
            modelBuilder.Entity<TouristRoutePhoto>().HasData(touristRoutePictures);

        }
    }
}
